package o;

import android.content.ContentUris;
import android.content.Context;
import android.content.ContextWrapper;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore.Audio;
import android.provider.MediaStore.Images.Media;
import android.provider.MediaStore.Video;
import android.text.TextUtils;
import java.util.Map.Entry;

public class dfv {
    public static boolean ˋ(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        try {
            String substring = str.substring(str.length() - 4);
            String substring2 = str.substring(str.length() - 5);
            if (".jpg".equalsIgnoreCase(substring) || ".jpeg".equalsIgnoreCase(substring2) || ".png".equalsIgnoreCase(substring) || ".bmp".equalsIgnoreCase(substring) || ".gif".equalsIgnoreCase(substring)) {
                return true;
            }
            return false;
        } catch (Throwable th) {
            return false;
        }
    }

    public static String ˏ(ContextWrapper contextWrapper, Uri uri) {
        String str = null;
        if (uri == null) {
            return null;
        }
        if (!uri.getScheme().equals("content")) {
            return uri.getPath();
        }
        Cursor query = contextWrapper.getContentResolver().query(uri, new String[]{"_data"}, null, null, null);
        if (query == null) {
            return null;
        }
        if (query.moveToFirst()) {
            str = query.getString(query.getColumnIndex("_data"));
        }
        query.close();
        return str;
    }

    public static String ˎ(Uri uri, Context context) {
        Uri uri2 = null;
        if ((VERSION.SDK_INT >= 19 ? 1 : 0) == 0 || !DocumentsContract.isDocumentUri(context, uri)) {
            if ("content".equalsIgnoreCase(uri.getScheme())) {
                return ˊ(context, uri, null, null);
            }
            if ("file".equalsIgnoreCase(uri.getScheme())) {
                return uri.getPath();
            }
            return null;
        } else if (ॱ(uri)) {
            r1 = DocumentsContract.getDocumentId(uri).split(":");
            if ("primary".equalsIgnoreCase(r1[0])) {
                return Environment.getExternalStorageDirectory() + "/" + r1[1];
            }
            if (TextUtils.isEmpty(ˎ(context))) {
                return null;
            }
            return ˎ(context) + "/" + r1[1];
        } else if (ˏ(uri)) {
            return ˊ(context, ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.parseLong(DocumentsContract.getDocumentId(uri))), null, null);
        } else if (!ˊ(uri)) {
            return null;
        } else {
            Object obj = DocumentsContract.getDocumentId(uri).split(":")[0];
            if ("image".equals(obj)) {
                uri2 = Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(obj)) {
                uri2 = Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(obj)) {
                uri2 = Audio.Media.EXTERNAL_CONTENT_URI;
            }
            String str = "_id=?";
            return ˊ(context, uri2, "_id=?", new String[]{r1[1]});
        }
    }

    private static boolean ॱ(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    private static String ˎ(Context context) {
        String str = "";
        Object obj = null;
        String str2 = "";
        for (Entry entry : System.getenv().entrySet()) {
            String str3 = (String) entry.getValue();
            String str4 = (String) entry.getKey();
            if ("SECONDARY_STORAGE".equals(str4)) {
                obj = 1;
                str2 = str3;
            }
            if (!"EXTERNAL_STORAGE".equals(str4)) {
                str3 = str;
            }
            str = str3;
        }
        return obj != null ? str2 : str;
    }

    private static boolean ˏ(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    private static String ˊ(Context context, Uri uri, String str, String[] strArr) {
        Throwable th;
        Cursor cursor = null;
        String str2 = "_data";
        try {
            Cursor query = context.getContentResolver().query(uri, new String[]{"_data"}, str, strArr, null);
            if (query != null) {
                try {
                    if (query.moveToFirst()) {
                        str2 = query.getString(query.getColumnIndexOrThrow("_data"));
                        if (query == null) {
                            return str2;
                        }
                        query.close();
                        return str2;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    cursor = query;
                    if (cursor != null) {
                        cursor.close();
                    }
                    throw th;
                }
            }
            if (query != null) {
                query.close();
            }
            return null;
        } catch (Throwable th3) {
            th = th3;
            if (cursor != null) {
                cursor.close();
            }
            throw th;
        }
    }

    private static boolean ˊ(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }
}
